
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.xas.spectrum &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.xas.spectrum</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.xas.spectrum</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module defines classes to represent all xas and stitching methods</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">scipy.interpolate</span> <span class="kn">import</span> <span class="n">interp1d</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.structure_matcher</span> <span class="kn">import</span> <span class="n">StructureMatcher</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.spectrum</span> <span class="kn">import</span> <span class="n">Spectrum</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Chen Zheng, Yiming Chen&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2012, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;3.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Yiming Chen&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;chz022@ucsd.edu, yic111@ucsd.edu&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;April 25, 2020&quot;</span>


<div class="viewcode-block" id="XAS"><a class="viewcode-back" href="../../../../pymatgen.analysis.xas.spectrum.html#pymatgen.analysis.xas.spectrum.XAS">[docs]</a><span class="k">class</span> <span class="nc">XAS</span><span class="p">(</span><span class="n">Spectrum</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Basic XAS object.</span>

<span class="sd">    Args:</span>
<span class="sd">        x: A sequence of x-ray energies in eV</span>
<span class="sd">        y: A sequence of mu(E)</span>
<span class="sd">        structure (Structure): Structure associated with the spectrum</span>
<span class="sd">        absorbing_element (Element): Element associated with the spectrum</span>
<span class="sd">        edge (str): Absorption edge associated with the spectrum</span>
<span class="sd">        spectrum_type (str): &#39;XANES&#39; or &#39;EXAFS&#39;</span>
<span class="sd">        absorbing_index (None or int): If None, the spectrum is assumed to be a</span>
<span class="sd">         site-weighted spectrum, which is comparable to experimental one.</span>
<span class="sd">         Otherwise, it indicates the absorbing_index for a site-wise spectrum.</span>


<span class="sd">    .. attribute: x</span>
<span class="sd">        The sequence of energies</span>

<span class="sd">    .. attribute: y</span>
<span class="sd">        The sequence of mu(E)</span>

<span class="sd">    .. attribute: absorbing_element</span>
<span class="sd">        The absorbing_element of the spectrum</span>

<span class="sd">    .. attribute: edge</span>
<span class="sd">        The edge of the spectrum</span>

<span class="sd">    .. attribute: spectrum_type</span>
<span class="sd">        XANES or EXAFS spectrum</span>

<span class="sd">    .. attribute: absorbing_index</span>
<span class="sd">        The absorbing_index of the spectrum</span>



<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">XLABEL</span> <span class="o">=</span> <span class="s1">&#39;Energy&#39;</span>
    <span class="n">YLABEL</span> <span class="o">=</span> <span class="s1">&#39;Intensity&#39;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">absorbing_element</span><span class="p">,</span> <span class="n">edge</span><span class="o">=</span><span class="s2">&quot;K&quot;</span><span class="p">,</span>
                 <span class="n">spectrum_type</span><span class="o">=</span><span class="s2">&quot;XANES&quot;</span><span class="p">,</span> <span class="n">absorbing_index</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes a spectrum object.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">XAS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">absorbing_element</span><span class="p">,</span>
                                  <span class="n">edge</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">structure</span> <span class="o">=</span> <span class="n">structure</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_element</span> <span class="o">=</span> <span class="n">absorbing_element</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">=</span> <span class="n">edge</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">=</span> <span class="n">spectrum_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">e0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">gradient</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span> <span class="o">/</span>
                                   <span class="n">np</span><span class="o">.</span><span class="n">gradient</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">))]</span>
        <span class="c1"># Wavenumber, k is calculated based on equation</span>
        <span class="c1">#             k^2=2*m/(hbar)^2*(E-E0)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">i</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">e0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.8537</span><span class="p">)</span> <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">e0</span> <span class="k">else</span>
                  <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">e0</span><span class="o">-</span><span class="n">i</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.8537</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_index</span> <span class="o">=</span> <span class="n">absorbing_index</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2"> Edge </span><span class="si">%s</span><span class="s2"> for </span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">,</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">XAS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span>
        <span class="p">)</span>

<div class="viewcode-block" id="XAS.stitch"><a class="viewcode-back" href="../../../../pymatgen.analysis.xas.spectrum.html#pymatgen.analysis.xas.spectrum.XAS.stitch">[docs]</a>    <span class="k">def</span> <span class="nf">stitch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="s1">&#39;XAS&#39;</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">500</span><span class="p">,</span> <span class="n">mode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;XAFS&quot;</span><span class="p">)</span> \
            <span class="o">-&gt;</span> <span class="s1">&#39;XAS&#39;</span><span class="p">:</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Stitch XAS objects to get the full XAFS spectrum or L23 edge XANES</span>
<span class="sd">        spectrum depending on the mode.</span>

<span class="sd">        1. Use XAFS mode for stitching XANES and EXAFS with same absorption edge.</span>
<span class="sd">            The stitching will be performed based on wavenumber, k.</span>
<span class="sd">            for k &lt;= 3, XAS(k) = XAS[XANES(k)]</span>
<span class="sd">            for 3 &lt; k &lt; max(xanes_k), will interpolate according to</span>
<span class="sd">                XAS(k)=f(k)*mu[XANES(k)]+(1-f(k))*mu[EXAFS(k)]</span>
<span class="sd">                where f(k)=cos^2((pi/2) (k-3)/(max(xanes_k)-3)</span>
<span class="sd">            for k &gt; max(xanes_k), XAS(k) = XAS[EXAFS(k)]</span>
<span class="sd">        2. Use L23 mode for stitching L2 and L3 edge XANES for elements with</span>
<span class="sd">            atomic number &lt;=30.</span>

<span class="sd">        Args:</span>
<span class="sd">            other: Another XAS object.</span>
<span class="sd">            num_samples(int): Number of samples for interpolation.</span>
<span class="sd">            mode(str): Either XAFS mode for stitching XANES and EXAFS</span>
<span class="sd">                        or L23 mode for stitching L2 and L3.</span>

<span class="sd">        Returns:</span>
<span class="sd">            XAS object: The stitched spectrum.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">StructureMatcher</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">m</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">structure</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">structure</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The input structures for spectra mismatch&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_element</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The absorbing elements for spectra are different&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_index</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">absorbing_index</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The absorbing indexes for spectra are different&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;XAFS&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">edge</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Only spectrum with the same absorption &quot;</span>
                                 <span class="s2">&quot;edge can be stitched in XAFS mode.&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">spectrum_type</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Need one XANES and one EXAFS spectrum to &quot;</span>
                                 <span class="s2">&quot;stitch in XAFS mode&quot;</span><span class="p">)</span>

            <span class="n">xanes</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">==</span> <span class="s2">&quot;XANES&quot;</span> <span class="k">else</span> <span class="n">other</span>
            <span class="n">exafs</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">==</span> <span class="s2">&quot;EXAFS&quot;</span> <span class="k">else</span> <span class="n">other</span>
            <span class="k">if</span> <span class="nb">max</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">x</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">min</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">x</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Energy overlap between XANES and EXAFS is needed for stitching&quot;</span><span class="p">)</span>

            <span class="c1"># for k &lt;= 3</span>
            <span class="n">wavenumber</span><span class="p">,</span> <span class="n">mu</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>  <span class="c1"># type: List[float],  List[float]</span>
            <span class="n">idx</span> <span class="o">=</span> <span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="p">),</span> <span class="n">x</span><span class="p">)))</span>
            <span class="n">mu</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">y</span><span class="p">[:</span><span class="n">idx</span><span class="p">])</span>
            <span class="n">wavenumber</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="p">[:</span><span class="n">idx</span><span class="p">])</span>

            <span class="c1"># for 3 &lt; k &lt; max(xanes.k)</span>
            <span class="n">fs</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># type: List[float]</span>
            <span class="n">ks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="p">),</span> <span class="mi">50</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ks</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">((</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span>
                <span class="n">fs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
            <span class="n">f_xanes</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
                <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">y</span><span class="p">),</span>
                <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">f_exafs</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
                <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">k</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">y</span><span class="p">),</span>
                <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">mu_xanes</span> <span class="o">=</span> <span class="n">f_xanes</span><span class="p">(</span><span class="n">ks</span><span class="p">)</span>
            <span class="n">mu_exafs</span> <span class="o">=</span> <span class="n">f_exafs</span><span class="p">(</span><span class="n">ks</span><span class="p">)</span>
            <span class="n">mus</span> <span class="o">=</span> <span class="p">[</span><span class="n">fs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_xanes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">fs</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">*</span> <span class="n">mu_exafs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span>
                   <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ks</span><span class="p">))]</span>
            <span class="n">mu</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">mus</span><span class="p">)</span>
            <span class="n">wavenumber</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">ks</span><span class="p">)</span>

            <span class="c1"># for k &gt; max(xanes.k)</span>
            <span class="n">idx</span> <span class="o">=</span> <span class="n">exafs</span><span class="o">.</span><span class="n">k</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">k</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="nb">max</span><span class="p">(</span><span class="n">xanes</span><span class="o">.</span><span class="n">k</span><span class="p">)))))</span>
            <span class="n">mu</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">idx</span><span class="p">:])</span>
            <span class="n">wavenumber</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exafs</span><span class="o">.</span><span class="n">k</span><span class="p">[</span><span class="n">idx</span><span class="p">:])</span>

            <span class="c1"># interpolation</span>
            <span class="n">f_final</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
                <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">wavenumber</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">mu</span><span class="p">),</span> <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">wavenumber_final</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">wavenumber</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">wavenumber</span><span class="p">),</span>
                                           <span class="n">num</span><span class="o">=</span><span class="n">num_samples</span><span class="p">)</span>
            <span class="n">mu_final</span> <span class="o">=</span> <span class="n">f_final</span><span class="p">(</span><span class="n">wavenumber_final</span><span class="p">)</span>
            <span class="n">energy_final</span> <span class="o">=</span> <span class="p">[</span><span class="mf">3.8537</span> <span class="o">*</span> <span class="n">i</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">xanes</span><span class="o">.</span><span class="n">e0</span> <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span>
                            <span class="o">-</span><span class="mf">3.8537</span> <span class="o">*</span> <span class="n">i</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">xanes</span><span class="o">.</span><span class="n">e0</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">wavenumber_final</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">XAS</span><span class="p">(</span><span class="n">energy_final</span><span class="p">,</span> <span class="n">mu_final</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure</span><span class="p">,</span>
                       <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">,</span> <span class="n">xanes</span><span class="o">.</span><span class="n">edge</span><span class="p">,</span> <span class="s2">&quot;XAFS&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;L23&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">!=</span> <span class="s2">&quot;XANES&quot;</span> <span class="ow">or</span> \
                    <span class="n">other</span><span class="o">.</span><span class="n">spectrum_type</span> <span class="o">!=</span> <span class="s2">&quot;XANES&quot;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Only XANES spectrum can be stitched in &quot;</span>
                                 <span class="s2">&quot;L23 mode.&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;L2&quot;</span><span class="p">,</span> <span class="s2">&quot;L3&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">edge</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;L2&quot;</span><span class="p">,</span> <span class="s2">&quot;L3&quot;</span><span class="p">]</span> \
                    <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">edge</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Need one L2 and one L3 edge spectrum to stitch&quot;</span>
                                 <span class="s2">&quot;in L23 mode.&quot;</span><span class="p">)</span>
            <span class="n">l2_xanes</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">==</span> <span class="s2">&quot;L2&quot;</span> <span class="k">else</span> <span class="n">other</span>
            <span class="n">l3_xanes</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">==</span> <span class="s2">&quot;L3&quot;</span> <span class="k">else</span> <span class="n">other</span>
            <span class="k">if</span> <span class="n">l2_xanes</span><span class="o">.</span><span class="n">absorbing_element</span><span class="o">.</span><span class="n">number</span> <span class="o">&gt;</span> <span class="mi">30</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Does not support L2,3-edge XANES for </span><span class="si">{}</span><span class="s2"> element&quot;</span>
                    <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">l2_xanes</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">))</span>

            <span class="n">l2_f</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
                <span class="n">l2_xanes</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">l2_xanes</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="c1"># will add value of l3.y[-1] to avoid sudden change in absorption coeff.</span>
            <span class="n">l3_f</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
                <span class="n">l3_xanes</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">l3_xanes</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="n">fill_value</span><span class="o">=</span><span class="n">l3_xanes</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">energy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">l3_xanes</span><span class="o">.</span><span class="n">x</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">l2_xanes</span><span class="o">.</span><span class="n">x</span><span class="p">),</span>
                                 <span class="n">num</span><span class="o">=</span><span class="n">num_samples</span><span class="p">)</span>
            <span class="n">mu</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">l2_f</span><span class="p">(</span><span class="n">energy</span><span class="p">),</span> <span class="n">l3_f</span><span class="p">(</span><span class="n">energy</span><span class="p">))]</span>
            <span class="k">return</span> <span class="n">XAS</span><span class="p">(</span><span class="n">energy</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">structure</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">,</span>
                       <span class="s2">&quot;L23&quot;</span><span class="p">,</span> <span class="s2">&quot;XANES&quot;</span><span class="p">)</span>

        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid mode. Only XAFS and L23 are supported.&quot;</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="site_weighted_spectrum"><a class="viewcode-back" href="../../../../pymatgen.analysis.xas.spectrum.html#pymatgen.analysis.xas.spectrum.site_weighted_spectrum">[docs]</a><span class="k">def</span> <span class="nf">site_weighted_spectrum</span><span class="p">(</span><span class="n">xas_list</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="s1">&#39;XAS&#39;</span><span class="p">],</span> <span class="n">num_samples</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">500</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s1">&#39;XAS&#39;</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Obtain site-weighted XAS object based on site multiplicity for each</span>
<span class="sd">        absorbing index and its corresponding site-wise spectrum.</span>

<span class="sd">        Args:</span>
<span class="sd">            xas_list([XAS]): List of XAS object to be weighted</span>
<span class="sd">            num_samples(int): Number of samples for interpolation</span>

<span class="sd">        Returns:</span>
<span class="sd">            XAS object: The site-weighted spectrum</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">StructureMatcher</span><span class="p">()</span>
    <span class="n">groups</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group_structures</span><span class="p">([</span><span class="n">i</span><span class="o">.</span><span class="n">structure</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">])</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">groups</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The input structures mismatch&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">({</span><span class="n">i</span><span class="o">.</span><span class="n">absorbing_element</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">})</span> <span class="o">==</span> \
            <span class="nb">len</span><span class="p">({</span><span class="n">i</span><span class="o">.</span><span class="n">edge</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">})</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Can only perform site-weighting for spectra with &quot;</span>
                         <span class="s2">&quot;same absorbing element and same absorbing edge.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">({</span><span class="n">i</span><span class="o">.</span><span class="n">absorbing_index</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">})</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> \
            <span class="kc">None</span> <span class="ow">in</span> <span class="p">{</span><span class="n">i</span><span class="o">.</span><span class="n">absorbing_index</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">}:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Need at least two site-wise spectra to perform &quot;</span>
                         <span class="s2">&quot;site-weighting&quot;</span><span class="p">)</span>

    <span class="n">sa</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">groups</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">ss</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">get_symmetrized_structure</span><span class="p">()</span>
    <span class="n">maxes</span><span class="p">,</span> <span class="n">mines</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="n">fs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">multiplicities</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">for</span> <span class="n">xas</span> <span class="ow">in</span> <span class="n">xas_list</span><span class="p">:</span>
        <span class="n">multiplicity</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span>
            <span class="n">ss</span><span class="o">.</span><span class="n">find_equivalent_sites</span><span class="p">(</span><span class="n">ss</span><span class="p">[</span><span class="n">xas</span><span class="o">.</span><span class="n">absorbing_index</span><span class="p">]))</span>
        <span class="n">multiplicities</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">)</span>
        <span class="n">maxes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">xas</span><span class="o">.</span><span class="n">x</span><span class="p">))</span>
        <span class="n">mines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">xas</span><span class="o">.</span><span class="n">x</span><span class="p">))</span>
        <span class="c1"># use 3rd-order spline interpolation for mu (idx 3) vs energy (idx 0).</span>
        <span class="n">f</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">(</span>
            <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">xas</span><span class="o">.</span><span class="n">x</span><span class="p">),</span>
            <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">xas</span><span class="o">.</span><span class="n">y</span><span class="p">),</span> <span class="n">bounds_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
            <span class="n">kind</span><span class="o">=</span><span class="s1">&#39;cubic&#39;</span><span class="p">)</span>
        <span class="n">fs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
    <span class="c1"># Interpolation within the intersection of x-axis ranges.</span>
    <span class="n">x_axis</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">mines</span><span class="p">),</span> <span class="nb">min</span><span class="p">(</span><span class="n">maxes</span><span class="p">),</span> <span class="n">num</span><span class="o">=</span><span class="n">num_samples</span><span class="p">)</span>
    <span class="n">weighted_spectrum</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">num_samples</span><span class="p">)</span>
    <span class="n">sum_multiplicities</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicities</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">multiplicities</span><span class="p">)):</span>
        <span class="n">weighted_spectrum</span> <span class="o">+=</span> <span class="p">(</span><span class="n">multiplicities</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">fs</span><span class="p">[</span><span class="n">i</span><span class="p">](</span><span class="n">x_axis</span><span class="p">))</span> \
                             <span class="o">/</span> <span class="n">sum_multiplicities</span>

    <span class="k">return</span> <span class="n">XAS</span><span class="p">(</span><span class="n">x_axis</span><span class="p">,</span> <span class="n">weighted_spectrum</span><span class="p">,</span> <span class="n">ss</span><span class="p">,</span> <span class="n">xas</span><span class="o">.</span><span class="n">absorbing_element</span><span class="p">,</span> <span class="n">xas</span><span class="o">.</span><span class="n">edge</span><span class="p">,</span>
               <span class="n">xas</span><span class="o">.</span><span class="n">spectrum_type</span><span class="p">)</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.xas.spectrum</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>